﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using System.Data;
using App.Data;
using System.Text;
using App.Core.DB;

public partial class Pages_Public_RentSearch : BasePage
{
    //private int _CountryID = 0;
    private int _CityID = 0;
    private int _ZoneID = 0;
    private int _RentTypeID = 0;
    private int _NoOfBedsFrom = 0;
    private int _NoOfBedsTo = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            BindSearchResult(1);
            BindDropdownLists();
        }
    }
    protected void BindPageInfo()
    {
        //_CountryID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.COUNTRY_ID);
        _CityID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.CITY_ID);
        _ZoneID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ZONE_ID);
        _RentTypeID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.RENT_TYPE_ID);
        String noOfBeds = WebUtil.GetQueryStringInString(AppConstants.QueryString.FROM_TO);
        if (!noOfBeds.IsNullOrEmpty())
        {
            String[] parts = noOfBeds.Split('-');
            if (parts.Length == 2)
            {
                _NoOfBedsFrom = parts[0].ToInt();
                _NoOfBedsTo = parts[1].ToInt();
            }
        }
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }
    protected void BindDropdownLists()
    {
        //BindDropdownList.Countries(ddlCountry);
        BindDropdownList.RentTypes(ddlRentType);
        //if (_CountryID > 0)
        //{
        //    ddlCountry.SetSelectedItem(_CountryID.ToString());
            BindDropdownList.Cities(ddlCity);
        //}
        if (_CityID > 0)
        {
            ddlCity.SetSelectedItem(_CityID.ToString());
            BindDropdownList.Zones(ddlZone, _CityID);
        }
        if (_ZoneID > 0)
        {
            ddlZone.SetSelectedItem(_ZoneID.ToString());
        }
        if (_RentTypeID > 0)
        ddlRentType.SetSelectedItem( _RentTypeID.ToString());
        ddlBedsFrom.SetSelectedItem(_NoOfBedsFrom.ToString());
        ddlBedsTo.SetSelectedItem(_NoOfBedsTo.ToString());
    }
    protected void BindSearchResult(int pageNumber)
    {
        int totalRecord = 0;
        UtilityDAO dao = new UtilityDAO();
        //DataSet ds = dao.GetPagedData(AppSQL.GET_PAGED_PROPERTIES_FOR_RENT, null, pageNumber, ConfigReader.PropertiesForRentListSize, out totalRecord);
        DataSet ds = dao.SearchPropertyForRent(_CityID, _ZoneID, _RentTypeID, pageNumber, ConfigReader.PropertiesForRentListSize, out totalRecord);
        StringBuilder sb = new StringBuilder();
        if (!ds.IsEmpty() && ds.Tables[0].Rows.Count > 0)
        {
            //String html = "<div class='ListItem'>{0}</div>";
            String imageHtml = "<img src='{0}/{1}' LargeImage='{0}/{2}' class='ThumbImageClickable' align='right' title='Click to View Large' alt='Click to View Large' onclick='ShowLargeImage(this);' />";
            StringBuilder listHTML = new StringBuilder();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                //sb.Length = 0;
                sb.Append("<div class='ListItem'>");
                sb.Append("<div class='ListColumn1'>");
                sb.AppendFormat("<div class='ListBoxHeading'>General Information</div>");
                sb.AppendFormat("<b>Size:</b> {0} SFT<br /><b>No of Beds:</b> {1} <br />", NullHandler.GetString(row["Size"]).HtmlEncode(), NullHandler.GetString(row["NoOfBeds"]));
                sb.AppendFormat("<b>Monthly Rent:</b> {0}<br /><b>Service Charge:</b> {1} <br />", NullHandler.GetString(row["MonthlyRent"]), NullHandler.GetString(row["ServiceCharge"]));
                sb.AppendFormat("<b>Address:</b> {0} <br />", NullHandler.GetString(row["Address"]).HtmlEncode());
                sb.Append("</div>");

                sb.Append("<div class='ListColumn2'>");
                sb.AppendFormat("<div class='ListBoxHeading'>Contact Information</div>");
                sb.AppendFormat("<b>Name:</b> {0}<br/>", NullHandler.GetString(row["CompanyName"]));
                String phone = NullHandler.GetString(row["Phone"]);
                String fax = NullHandler.GetString(row["Fax"]);
                String email = NullHandler.GetString(row["Email"]);
                sb.AppendFormat("<b>Phone:</b> {0} <br/>", phone.IsNullOrEmpty() ? "N/A" : phone.HtmlEncode());
                sb.AppendFormat("<b>Fax:</b> {0} <br/>", fax.IsNullOrEmpty() ? "N/A" : fax.HtmlEncode());
                sb.AppendFormat("<b>Email:</b> {0} <br/>", email.IsNullOrEmpty() ? "N/A" : email.HtmlEncode());
                //listHTML.AppendFormat(html, sb.ToString());
                //listHTML.AppendFormat("<div class='clearboth'></div>");
                sb.Append("</div>");

                String thumbImage = NullHandler.GetString(row["ThumbImage"]);
                if (!thumbImage.IsNullOrEmpty())
                {
                    sb.Append("<div class='ListColumn3'>");
                    sb.AppendFormat(imageHtml, AppConstants.FOR_RENT_PROPERTY_FILES, thumbImage, NullHandler.GetString(row["LargeImage"]));
                    sb.Append("</div>");
                }
                sb.Append("<div class='clearboth'></div>");
                sb.AppendFormat("<div><b>Description:</b> {0}</div>", WebUtil.FormatText(NullHandler.GetString(row["Remark"])));
                sb.AppendFormat("<b>Will Expire On:</b> {0}", NullHandler.GetDateTime(row["ExpireDate"]).ToString(AppConstants.ValueOf.DATE_FROMAT_DISPLAY));
                sb.Append("</div>");
            }
            ltrListing.Text = sb.ToString();
        }
        else
            WebUtil.ShowMessageBox(divNoRecordMessage, "Sorry! No Record Found.", true);
        
        ///Bind Pager
        ucPagerBottom.TotalRecord = totalRecord;
        ucPagerBottom.PageSize = ConfigReader.PropertiesForRentListSize;
        ucPagerBottom.PageNo = pageNumber;
        ucPagerBottom.TotalRecordMessage = "Total {0} Properties Found.";
        ucPagerBottom.DataBind();
    }
    protected void ucPagerBottom_PageIndexChanged(object sender,  PagerEventArgs e)
    {
        BindSearchResult(e.PageIndex);
    }
}